<?php

defined('SYSPATH') or die('No direct script access.');

class Task_Pool extends Minion_Task {

	/**
	 * Test action
	 *
	 * @param array $params
	 * @return void
	 */
	protected function _execute(array $params) {
		Minion_CLI::write(Minion_CLI::color('Getting PoolStats started at ' . date('H:i:s'), 'green'));

		$login = 'vlgafox111';
		$key = 'ef946d4aa68547a3ad5a7d1d56daf23a';
		$secret = '494061d83efd4e77ae94e1f06ba25713';
		$nonce = time();
		$signature = strtoupper(hash_hmac('sha256', $login . $key . $nonce, $secret));

		$post = array(
			'key' => $key,
			'nonce' => $nonce,
			'signature' => $signature,
		);

		$request = Request::factory('https://antpool.com/api/poolStats.htm')
				->method('POST')
				->post($post);
		$response = json_decode($request->execute());
		if ($response->message == "ok") {
			Minion_CLI::write(Minion_CLI::color('Pool Stats get OK', 'green'));
			$data['poolstats'] = (array) $response->data;
		}
		else {
			Minion_CLI::write(Minion_CLI::color('Pool Stats get ERROR: ' . $response->message, 'red'));
		}

		$request = Request::factory('https://antpool.com/api/account.htm')
				->method('POST')
				->post($post);
		$response = json_decode($request->execute());
		if ($response->message == "ok") {
			Minion_CLI::write(Minion_CLI::color('Account balance get OK', 'green'));
			$data['account'] = (array) $response->data;
		}
		else {
			Minion_CLI::write(Minion_CLI::color('Account balance get ERROR: ' . $response->message, 'red'));
		}


		$request = Request::factory('https://antpool.com/api/hashrate.htm')
				->method('POST')
				->post($post);
		$response = json_decode($request->execute());
		if ($response->message == "ok") {
			Minion_CLI::write(Minion_CLI::color('Hash Rate get OK', 'green'));
			$data['hashrate'] = (array) $response->data;
		}
		else {
			Minion_CLI::write(Minion_CLI::color('Hash Rate get ERROR: ' . $response->message, 'red'));
		}

		$post['pageEnable'] = 0;
		$post['page'] = 1;
		$post['pageSize'] = 10;
		$request = Request::factory('https://antpool.com/api/workers.htm')
				->method('POST')
				->post($post);
		$response = json_decode($request->execute());
		if ($response->message == "ok") {
			Minion_CLI::write(Minion_CLI::color('Workers\' Hash Rate get OK', 'green'));
			$data['workers'] = (array) $response->data;
		}
		else {
			Minion_CLI::write(Minion_CLI::color('Workers\' Hash Rate get ERROR: ' . $response->message, 'red'));
		}

		$post['type'] = 'pplns';
		$request = Request::factory('https://antpool.com/api/paymentHistory.htm')
				->method('POST')
				->post($post);
		$response = json_decode($request->execute());
		if ($response->message == "ok") {
			Minion_CLI::write(Minion_CLI::color('Payment History get OK', 'green'));
			$data['paymentHistory'] = (array) $response->data;
		}
		else {
			Minion_CLI::write(Minion_CLI::color('Payment History get ERROR: ' . $response->message, 'red'));
		}
		$poolstats = View::factory('pool/stats', $data);
		file_put_contents(DOCROOT.'application/views/pool/statsistics.php','<?php defined(\'SYSPATH\') or die(\'No direct script access.\') ?>'.$poolstats);
		//echoModel::factory('Book')->set_config('poolstats',$poolstats);

		Minion_CLI::write(Minion_CLI::color('Getting PoolStats ended at ' . date('H:i:s'), 'green'));
	}

}

// End Sendmail